package com.base.commons.tools.utils;

import org.springframework.context.i18n.LocaleContextHolder;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author dmain
 * @version 1.00
 * @time 2019/10/25 0025
 */
public class BusinessUtils {

    private static ScriptEngineManager sem = new ScriptEngineManager();
    private static final String CN = "CN";

    /**
     * 倒序集合
     * @param sortList
     */
    public static void sortCollection(List<BigDecimal> sortList){
        Collections.sort(sortList, new Comparator<BigDecimal>() {
            @Override
            public int compare(BigDecimal o1, BigDecimal o2) {
                return (o1.compareTo(o2)==1 ? -1 : 1);
            }
        });
    }

    public static Object calculateExpression(String exp)throws ScriptException {
        ScriptEngine scriptEngine = sem.getEngineByName("js");
        Object obj = scriptEngine.eval(exp);
        return obj;
    }

    public static String getLanguageCountry(){
        String languageCountry = LocaleContextHolder.getLocale().getCountry();
        return StringUtils.isEmpty(languageCountry) ? CN : languageCountry;
    }

}
